package com.ruoyi.supermarket.service.impl;

import java.util.List;

import com.ruoyi.common.enums.ShopStatusType;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.ShopMapper;
import com.ruoyi.supermarket.domain.Shop;
import com.ruoyi.supermarket.service.ShopService;
import org.springframework.util.CollectionUtils;

/**
 * 我的店铺Service业务层处理
 * 
 * @author liqiwei
 * @date 2023-10-03
 */
@Service
public class ShopServiceImpl implements ShopService
{
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 查询我的店铺
     * 
     * @param id 我的店铺主键
     * @return 我的店铺
     */
    @Override
    public Shop selectShopById(Long id)
    {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询登录用户店铺
     *
     * @param userId 用户ID
     * @return 店铺ID
     */
    @Override
    public Long selectShopByUserId(Long userId) {
        Shop shop = new Shop();
        shop.setUserId(userId);
        List<Shop> shopList =shopMapper.selectShopList(shop);
        if(CollectionUtils.isEmpty(shopList)){
            throw new ServiceException("查询不到用户店铺数据");
        }else {
            shop = shopList.get(0);
            if(ShopStatusType.SUCCESS.getStatus().equals(shop.getShopStatus())){
                return shop.getId();
            }else {
                throw new ServiceException("用户店铺异常，请进入店铺管理，查看我的店铺状态");
            }
        }
    }

    /**
     * 查询我的店铺列表
     * 
     * @param shop 我的店铺
     * @return 我的店铺
     */
    @Override
    public List<Shop> selectShopList(Shop shop)
    {
        return shopMapper.selectShopList(shop);
    }

    /**
     * 新增我的店铺
     * 
     * @param shop 我的店铺
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop)
    {
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改我的店铺
     * 
     * @param shop 我的店铺
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop)
    {
        return shopMapper.updateShop(shop);
    }

    /**
     * 审核我的店铺
     *
     * @param shop 我的店铺
     * @return 结果
     */
    @Override
    public int auditShop(Shop shop) {
        Shop s = new Shop();
        s.setUscc(shop.getUscc());
        List<Shop> shopList = shopMapper.selectShopList(s);

        s = shopList.get(0);
        s.setShopStatus(shop.getShopStatus());
        return shopMapper.updateShop(s);
    }

    /**
     * 批量删除我的店铺
     * 
     * @param ids 需要删除的我的店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids)
    {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除我的店铺信息
     * 
     * @param id 我的店铺主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id)
    {
        return shopMapper.deleteShopById(id);
    }
}
